/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PSearch.java
 *
 * Created on Mar 26, 2012, 11:16:08 AM
 */
package com.bc5Neptune.cis.gui;

/**
 *
 * @author phu.huynh
 */
import static com.googlecode.javacv.cpp.opencv_core.*;
import static com.googlecode.javacv.cpp.opencv_imgproc.*;
import static com.googlecode.javacv.cpp.opencv_highgui.*;
import com.bc5Neptune.cis.bll.HorizontalIconRender;
import com.bc5Neptune.cis.bll.FaceDetection;
import java.awt.event.InputEvent;
import java.awt.event.MouseEvent;
import com.bc5Neptune.cis.bll.IconList;
import com.bc5Neptune.cis.bll.ProcessFile;
import com.bc5Neptune.cis.bll.ProcessImage;
import com.bc5Neptune.cis.bll.VerticalIconRender;
import com.bc5Neptune.cis.config.ConnectDB2;
import com.bc5Neptune.cis.dal.PersonDAL;
import com.bc5Neptune.cis.entity.*;
import com.googlecode.javacv.cpp.opencv_core.IplImage;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JList;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import static com.googlecode.javacv.cpp.opencv_core.IPL_DEPTH_8U;
import com.googlecode.javacv.cpp.opencv_core.IplImage;
import static com.googlecode.javacv.cpp.opencv_highgui.cvLoadImage;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashSet;
import java.util.List;

public final class PSearch extends javax.swing.JPanel {

    /*
     * the object of PDetelePerson
     */
    // public PDeletePerson deletePerson = new PDeletePerson();
    RSTableModel elementTable;
    ConnectDB2 obj = new ConnectDB2();
    PersonDAL a = new PersonDAL();
    public ArrayList<IconList> iconArr = new ArrayList<IconList>();
    String groupRes;
    String groupHTown;
    String tempPath;
    /**
     * Creates new form PSearch
     */
    public PSearch() {

        initComponents();
        pnlEdit.setVisible(false);
        pnlResult.setVisible(false);
        initialInformationForm();

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jFileChooser1 = new javax.swing.JFileChooser();
        jSplitPane1 = new javax.swing.JSplitPane();
        jPanel2 = new javax.swing.JPanel();
        btnSave = new javax.swing.JButton();
        btnRestore = new javax.swing.JButton();
        filler1 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 0), new java.awt.Dimension(32767, 0));
        jPanel3 = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jSplitPane2 = new javax.swing.JSplitPane();
        jPanel5 = new javax.swing.JPanel();
        jSplitPane3 = new javax.swing.JSplitPane();
        jPanel7 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        txfitem = new javax.swing.JTextField();
        cbxsearch = new javax.swing.JComboBox();
        btnsearch = new javax.swing.JButton();
        pnlResult = new javax.swing.JPanel();
        btnDelete = new javax.swing.JButton();
        btnEdit = new javax.swing.JButton();
        jScrollPane4 = new javax.swing.JScrollPane();
        tblPersonInfor = new javax.swing.JTable();
        jPanel8 = new javax.swing.JPanel();
        pnlEdit = new javax.swing.JPanel();
        jScrollPane8 = new javax.swing.JScrollPane();
        txfResidence = new javax.swing.JTextArea();
        jLabel3 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane9 = new javax.swing.JScrollPane();
        txfHometown = new javax.swing.JTextArea();
        jLabel4 = new javax.swing.JLabel();
        txfReligion = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        txfEthnic = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        txfIdentifyNumber = new javax.swing.JTextField();
        txfDob = new javax.swing.JTextField();
        txfFullname = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        lblLoadImg = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txaCharac = new javax.swing.JTextArea();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        cbbHTownPrv = new javax.swing.JComboBox();
        cbbHTownDis = new javax.swing.JComboBox();
        cbbHTownWrd = new javax.swing.JComboBox();
        cbbHTownGrp = new javax.swing.JComboBox();
        cbbResidencePrv = new javax.swing.JComboBox();
        cbbResidenceDis = new javax.swing.JComboBox();
        cbbResidenceWrd = new javax.swing.JComboBox();
        cbbResidenceGrp = new javax.swing.JComboBox();
        jLabel12 = new javax.swing.JLabel();
        jPanel6 = new javax.swing.JPanel();
        scrollListFace = new javax.swing.JScrollPane();
        lstFace = new javax.swing.JList();

        setName("Information Searching"); // NOI18N
        setLayout(new java.awt.BorderLayout());

        jSplitPane1.setDividerLocation(50);
        jSplitPane1.setDividerSize(2);
        jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);

        jPanel2.setBackground(java.awt.Color.gray);

        btnSave.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/must_have_icon_set/Save/Save_16x16.png"))); // NOI18N
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });

        btnRestore.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/must_have_icon_set/Undo/Undo_16x16.png"))); // NOI18N
        btnRestore.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRestoreActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(filler1, javax.swing.GroupLayout.PREFERRED_SIZE, 709, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(229, 229, 229)
                .addComponent(btnSave)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnRestore)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(21, 21, 21)
                        .addComponent(filler1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(btnSave)
                            .addComponent(btnRestore))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jSplitPane1.setTopComponent(jPanel2);

        jPanel3.setLayout(new java.awt.BorderLayout());

        jPanel1.setLayout(new java.awt.BorderLayout());

        jSplitPane2.setDividerLocation(470);
        jSplitPane2.setDividerSize(2);
        jSplitPane2.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);

        jPanel5.setLayout(new java.awt.BorderLayout());

        jSplitPane3.setDividerLocation(320);
        jSplitPane3.setDividerSize(2);

        jPanel4.setBorder(null);

        jLabel2.setText("Search Type:");

        txfitem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txfitemActionPerformed(evt);
            }
        });

        cbxsearch.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Identity number", "Name" }));
        cbxsearch.setToolTipText("");
        cbxsearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbxsearchActionPerformed(evt);
            }
        });

        btnsearch.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/must_have_icon_set/Search/Search_16x16.png"))); // NOI18N
        btnsearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnsearchActionPerformed(evt);
            }
        });

        pnlResult.setBorder(null);

        btnDelete.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/must_have_icon_set/Delete/Delete_16x16.png"))); // NOI18N
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });

        btnEdit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon/must_have_icon_set/Edit/Edit_16x16.png"))); // NOI18N
        btnEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditActionPerformed(evt);
            }
        });

        tblPersonInfor.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null}
            },
            new String [] {
                "PID", "FullName", "Identity Number", "Dob"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jScrollPane4.setViewportView(tblPersonInfor);

        javax.swing.GroupLayout pnlResultLayout = new javax.swing.GroupLayout(pnlResult);
        pnlResult.setLayout(pnlResultLayout);
        pnlResultLayout.setHorizontalGroup(
            pnlResultLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlResultLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlResultLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 306, Short.MAX_VALUE)
                    .addGroup(pnlResultLayout.createSequentialGroup()
                        .addComponent(btnEdit, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnDelete, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        pnlResultLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btnDelete, btnEdit});

        pnlResultLayout.setVerticalGroup(
            pnlResultLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlResultLayout.createSequentialGroup()
                .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 326, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlResultLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(btnEdit, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnDelete, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(33, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(7, 7, 7)
                        .addComponent(cbxsearch, javax.swing.GroupLayout.PREFERRED_SIZE, 159, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(txfitem, javax.swing.GroupLayout.PREFERRED_SIZE, 252, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnsearch, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addComponent(pnlResult, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(btnsearch)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(cbxsearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txfitem, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addComponent(pnlResult, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7);
        jPanel7.setLayout(jPanel7Layout);
        jPanel7Layout.setHorizontalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel7Layout.setVerticalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jSplitPane3.setLeftComponent(jPanel7);

        pnlEdit.setBackground(java.awt.Color.gray);
        pnlEdit.setBorder(null);

        txfResidence.setColumns(10);
        txfResidence.setRows(2);
        txfResidence.setTabSize(5);
        jScrollPane8.setViewportView(txfResidence);

        jLabel3.setForeground(java.awt.Color.white);
        jLabel3.setText("Full name:");

        jLabel1.setForeground(java.awt.Color.white);
        jLabel1.setText("Identity number:");

        txfHometown.setColumns(10);
        txfHometown.setRows(3);
        txfHometown.setTabSize(5);
        jScrollPane9.setViewportView(txfHometown);

        jLabel4.setForeground(java.awt.Color.white);
        jLabel4.setText("Hometown:");

        jLabel5.setForeground(java.awt.Color.white);
        jLabel5.setText("Birthday:");

        jLabel6.setForeground(java.awt.Color.white);
        jLabel6.setText("Ethnic:");

        jLabel7.setForeground(java.awt.Color.white);
        jLabel7.setText("Place of residence:");

        jLabel8.setForeground(java.awt.Color.white);
        jLabel8.setText("Religion:");

        lblLoadImg.setForeground(java.awt.Color.white);

        jLabel9.setForeground(java.awt.Color.white);
        jLabel9.setText("Characteristic:");

        txaCharac.setColumns(10);
        txaCharac.setRows(3);
        jScrollPane1.setViewportView(txaCharac);

        jLabel10.setForeground(java.awt.Color.white);
        jLabel10.setText("Hometown:");

        jLabel11.setForeground(java.awt.Color.white);
        jLabel11.setText("Place of residence:");

        cbbHTownPrv.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "--Select Province--" }));

        cbbHTownDis.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "--Select District--" }));

        cbbHTownWrd.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "--Select Ward--" }));

        cbbHTownGrp.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "--Select Group--" }));

        cbbResidencePrv.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "--Select Province--" }));

        cbbResidenceDis.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "--Select District--" }));
        cbbResidenceDis.setName(""); // NOI18N

        cbbResidenceWrd.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "--Select Ward--" }));

        cbbResidenceGrp.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "--Select Group--" }));

        jLabel12.setForeground(java.awt.Color.white);
        jLabel12.setText("Click here to import image");
        jLabel12.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel12MouseClicked(evt);
            }
        });

        javax.swing.GroupLayout pnlEditLayout = new javax.swing.GroupLayout(pnlEdit);
        pnlEdit.setLayout(pnlEditLayout);
        pnlEditLayout.setHorizontalGroup(
            pnlEditLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlEditLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlEditLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlEditLayout.createSequentialGroup()
                        .addComponent(lblLoadImg, javax.swing.GroupLayout.PREFERRED_SIZE, 316, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(pnlEditLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel3)
                            .addComponent(jLabel5)
                            .addComponent(jLabel9)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlEditLayout.createSequentialGroup()
                                .addGroup(pnlEditLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(txfDob, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 322, Short.MAX_VALUE)
                                    .addComponent(txfFullname, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 322, Short.MAX_VALUE)
                                    .addComponent(txfIdentifyNumber, javax.swing.GroupLayout.DEFAULT_SIZE, 322, Short.MAX_VALUE))
                                .addContainerGap())))
                    .addGroup(pnlEditLayout.createSequentialGroup()
                        .addGroup(pnlEditLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(pnlEditLayout.createSequentialGroup()
                                .addGap(57, 57, 57)
                                .addComponent(jLabel12))
                            .addGroup(pnlEditLayout.createSequentialGroup()
                                .addComponent(jLabel6)
                                .addGap(108, 108, 108)
                                .addComponent(jLabel8))
                            .addGroup(pnlEditLayout.createSequentialGroup()
                                .addComponent(txfEthnic, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(12, 12, 12)
                                .addComponent(txfReligion, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(26, 26, 26)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 322, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(pnlEditLayout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addGap(255, 255, 255)
                        .addComponent(jLabel7))
                    .addGroup(pnlEditLayout.createSequentialGroup()
                        .addComponent(jScrollPane9, javax.swing.GroupLayout.PREFERRED_SIZE, 308, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(26, 26, 26)
                        .addComponent(jScrollPane8, javax.swing.GroupLayout.DEFAULT_SIZE, 322, Short.MAX_VALUE)
                        .addContainerGap())
                    .addComponent(jLabel10)
                    .addGroup(pnlEditLayout.createSequentialGroup()
                        .addComponent(cbbHTownPrv, 0, 156, Short.MAX_VALUE)
                        .addGap(6, 6, 6)
                        .addComponent(cbbHTownDis, 0, 156, Short.MAX_VALUE)
                        .addGap(6, 6, 6)
                        .addComponent(cbbHTownWrd, 0, 156, Short.MAX_VALUE)
                        .addGap(6, 6, 6)
                        .addComponent(cbbHTownGrp, 0, 157, Short.MAX_VALUE)
                        .addGap(25, 25, 25))
                    .addComponent(jLabel11)
                    .addGroup(pnlEditLayout.createSequentialGroup()
                        .addComponent(cbbResidencePrv, 0, 156, Short.MAX_VALUE)
                        .addGap(6, 6, 6)
                        .addComponent(cbbResidenceDis, 0, 156, Short.MAX_VALUE)
                        .addGap(6, 6, 6)
                        .addComponent(cbbResidenceWrd, 0, 156, Short.MAX_VALUE)
                        .addGap(6, 6, 6)
                        .addComponent(cbbResidenceGrp, 0, 157, Short.MAX_VALUE)
                        .addGap(25, 25, 25))))
        );
        pnlEditLayout.setVerticalGroup(
            pnlEditLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlEditLayout.createSequentialGroup()
                .addGap(12, 12, 12)
                .addGroup(pnlEditLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblLoadImg, javax.swing.GroupLayout.PREFERRED_SIZE, 178, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(pnlEditLayout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(8, 8, 8)
                        .addComponent(txfIdentifyNumber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(4, 4, 4)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txfFullname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txfDob, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel9)))
                .addGap(6, 6, 6)
                .addGroup(pnlEditLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlEditLayout.createSequentialGroup()
                        .addComponent(jLabel12)
                        .addGap(6, 6, 6)
                        .addGroup(pnlEditLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(pnlEditLayout.createSequentialGroup()
                                .addGap(2, 2, 2)
                                .addComponent(jLabel6))
                            .addComponent(jLabel8))
                        .addGroup(pnlEditLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txfEthnic, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txfReligion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(pnlEditLayout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnlEditLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(jLabel7))
                .addGap(6, 6, 6)
                .addGroup(pnlEditLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane8)
                    .addComponent(jScrollPane9))
                .addGap(6, 6, 6)
                .addComponent(jLabel10)
                .addGap(6, 6, 6)
                .addGroup(pnlEditLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cbbHTownPrv, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbbHTownDis, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbbHTownWrd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbbHTownGrp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(12, 12, 12)
                .addComponent(jLabel11)
                .addGap(6, 6, 6)
                .addGroup(pnlEditLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cbbResidencePrv, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbbResidenceDis, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbbResidenceWrd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbbResidenceGrp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        javax.swing.GroupLayout jPanel8Layout = new javax.swing.GroupLayout(jPanel8);
        jPanel8.setLayout(jPanel8Layout);
        jPanel8Layout.setHorizontalGroup(
            jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pnlEdit, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel8Layout.setVerticalGroup(
            jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pnlEdit, javax.swing.GroupLayout.DEFAULT_SIZE, 481, Short.MAX_VALUE)
        );

        jSplitPane3.setRightComponent(jPanel8);

        jPanel5.add(jSplitPane3, java.awt.BorderLayout.CENTER);

        jSplitPane2.setTopComponent(jPanel5);

        lstFace.setLayoutOrientation(javax.swing.JList.HORIZONTAL_WRAP);
        lstFace.setVisibleRowCount(1);
        scrollListFace.setViewportView(lstFace);

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(scrollListFace, javax.swing.GroupLayout.DEFAULT_SIZE, 1012, Short.MAX_VALUE)
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(scrollListFace, javax.swing.GroupLayout.DEFAULT_SIZE, 135, Short.MAX_VALUE)
        );

        jSplitPane2.setRightComponent(jPanel6);

        jPanel1.add(jSplitPane2, java.awt.BorderLayout.CENTER);

        jPanel3.add(jPanel1, java.awt.BorderLayout.CENTER);

        jSplitPane1.setRightComponent(jPanel3);

        add(jSplitPane1, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditActionPerformed
        //visible pnlEdit
        pnlEdit.setVisible(true);
        //load data to edit
        loadData(getIdentityNumber());

    }//GEN-LAST:event_btnEditActionPerformed

    public void loadData(String id) {
        try {
            PersonDAL objPersonDAL = new PersonDAL();

            //txfPid.setText(obj2.getPid());

            //    String id = txfPid.getText();
            //   System.out.println("id la "+id);
            System.out.println("Id of person: " + id);

            String dob = objPersonDAL.Select(id).getDob().toString();
            txfDob.setText(dob);
            String identifyNumber = objPersonDAL.Select(id).getIdentity_number();
            txfIdentifyNumber.setText(identifyNumber);

            String ethnic = objPersonDAL.Select(id).getEthnic();
            txfEthnic.setText(ethnic);
            String name = objPersonDAL.Select(id).getFullname();
            txfFullname.setText(name);
            String hometown = objPersonDAL.Select(id).getHometown();
            txfHometown.setText(hometown);
            String religion = objPersonDAL.Select(id).getReligion();
            txfReligion.setText(religion);
            String res = objPersonDAL.Select(id).getPermanent_residence();
            txfResidence.setText(res);
            //load image
            BufferedImage bufferedImage = ImageIO.read(new PersonDAL().Select(id).getImage().getBinaryStream());
            bufferedImage = new ProcessImage().resize(bufferedImage, 240, 200);
            lblLoadImg.setIcon(new ImageIcon(bufferedImage));

            //load face data
            loadFaceData();
        } catch (SQLException ex) {
            Logger.getLogger(PSearch.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(PSearch.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public String getIdentityNumber() {
        String identity = null;

        int i = tblPersonInfor.getSelectedRow();
        identity = tblPersonInfor.getValueAt(i, 2).toString();
        System.out.println(identity);
        return identity;
    }
    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed
        // TODO add your handling code here:

        //  deletePerson.setModel(elementTable);
        //  GApplication.instance.addTab("Delete Person", "Open Delete Person", deletePerson, GApplication.mainTab);
        String id = getPid();
        if (deleteByID(id)) {
            JOptionPane.showConfirmDialog(this, "Can not delete this person! Please try again!",
                    "WARNING", JOptionPane.CLOSED_OPTION,
                    JOptionPane.ERROR_MESSAGE);
            return;
        } else {
            JOptionPane.showConfirmDialog(this, "Person is deleted!",
                    "NOTICE", JOptionPane.CLOSED_OPTION,
                    JOptionPane.INFORMATION_MESSAGE);
            return;
        }

    }//GEN-LAST:event_btnDeleteActionPerformed

    public boolean deleteByID(String id) {
        boolean result = false;
        PersonDAL obj2 = new PersonDAL();
        obj2.deleteID(id);
        //remove at table
        int i = tblPersonInfor.getSelectedRow();
        tblPersonInfor.remove(i);
        tblPersonInfor.repaint();
        return result;
    }

    public void loadFaceData() {
        ProcessFile objFile = new ProcessFile();
        iconArr.clear(); //ensure empty
        String id = txfIdentifyNumber.getText();
        for (int i = 0; i < 10; i++) {
            BufferedImage imageIcon = objFile.loadBufferedFace("../CIS_SProjectR2/data/facedat/",
                    id, i);
            iconArr.add(new IconList(id, imageIcon, 92, 112));

        }
        updateFaceCellRender();
    }

    public void updateFaceCellRender() {
        // iconArr[0] = new IconList("Recognition", imageIcon, 20, 30);
        lstFace = new JList(iconArr.toArray());

        HorizontalIconRender render = new HorizontalIconRender();

        lstFace.setCellRenderer(render);
        lstFace.setFixedCellHeight(120);
        //set sell width
        lstFace.setFixedCellWidth(200);
        scrollListFace.setViewportView(lstFace);
        lstFace.addMouseListener(new ListFaceDataListener(this));
    }

private void cbxsearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbxsearchActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_cbxsearchActionPerformed

private void btnsearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnsearchActionPerformed
// TODO add your handling code here:
    search();
    pnlResult.setVisible(true);

}//GEN-LAST:event_btnsearchActionPerformed

    public void search() {
        obj.getConnection();
        int id = cbxsearch.getSelectedIndex();
        int name = cbxsearch.getSelectedIndex();
        String item = txfitem.getText();
        if (item.isEmpty() == true) {
            JOptionPane.showConfirmDialog(this, "Please enter id or name!",
                    "Warning", JOptionPane.CLOSED_OPTION,
                    JOptionPane.WARNING_MESSAGE);
            return;
        }
        if (id == 0) {
            elementTable = searchByID(txfitem.getText());
            tblPersonInfor.setModel(elementTable);
        } else if (name == 1) {
            elementTable = searchByName(txfitem.getText());
            tblPersonInfor.setModel(elementTable);
        }
    }

    public RSTableModel searchByID(String id) {
        RSTableModel tableModel = null;
        try {
            ResultSet rs = a.searchid(id);
            tableModel = new RSTableModel(rs);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return tableModel;

    }

    public RSTableModel searchByName(String name) {
        RSTableModel tableModel = null;
        try {
            ResultSet rs1 = a.searchname(name);
            tableModel = new RSTableModel(rs1);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return tableModel;
    }

    public String getPid() {
        String id = null;

        int i = tblPersonInfor.getSelectedRow();
        id = tblPersonInfor.getValueAt(i, 0).toString();
        System.out.println(id);
        return id;
    }
private void txfitemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txfitemActionPerformed
// TODO add your handling code here:
    btnsearchActionPerformed(evt);
}//GEN-LAST:event_txfitemActionPerformed

    private void btnRestoreActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRestoreActionPerformed
        // TODO add your handling code here:
        pnlEdit.setVisible(false);
        iconArr.clear();
        updateFaceCellRender();
    }//GEN-LAST:event_btnRestoreActionPerformed
    E2 ee2 = new E2();
    E3 ee3 = new E3();
    E4 ee4 = new E4();
    //------------------------
    E6 ee6 = new E6();
    E7 ee7 = new E7();
    E8 ee8 = new E8();

    private void initialInformationForm() {
        // initialize list entity of CountryEntity
        PersonDAL prsDal = new PersonDAL();
        List<ProvinceEntity> listEntity = prsDal.getListProvince();
        for (ProvinceEntity Province : listEntity) {
            cbbHTownPrv.addItem(Province);
            cbbResidencePrv.addItem(Province);
        }
        cbbHTownPrv.setSelectedIndex(2);
        // cbbResidencePrv.setSelectedItem();
        FilterProvinceHTown ee1 = new FilterProvinceHTown();
        cbbHTownPrv.addActionListener(ee1);

        cbbHTownDis.addActionListener(ee2);

        cbbHTownWrd.addActionListener(ee3);

        cbbHTownGrp.addActionListener(ee4);

        FilterProvinceRes ee5 = new FilterProvinceRes();
        cbbResidencePrv.addActionListener(ee5);
        cbbResidenceDis.addActionListener(ee6);
        cbbResidenceWrd.addActionListener(ee7);
        cbbResidenceGrp.addActionListener(ee8);

    }

    public int removeE2() {
        int i = 0;
        if (cbbHTownPrv.getSelectedIndex() == 0) {
            cbbHTownDis.removeActionListener(ee2);
            cbbHTownDis.removeAllItems();
            cbbHTownDis.addItem("--Select District--");
            cbbHTownDis.addActionListener(ee2);
        } else {
            if (cbbHTownDis.getItemAt(1) != null) {
                cbbHTownDis.removeActionListener(ee2);
                cbbHTownDis.removeAllItems();
                cbbHTownDis.addItem("--Select District--");
                cbbHTownDis.addActionListener(ee2);
                //i =1;
            }
            i = 1;
        }

        return i;
    }

    public void removeE3() {
        if (cbbHTownWrd.getItemAt(1) != null) {
            cbbHTownWrd.removeActionListener(ee3);
            cbbHTownWrd.removeAllItems();
            cbbHTownWrd.addItem("--Select Ward--");
            cbbHTownWrd.addActionListener(ee3);
        }
    }

    public void removeE4() {
        if (cbbHTownGrp.getItemAt(1) != null) {
            cbbHTownGrp.removeActionListener(ee4);
            cbbHTownGrp.removeAllItems();
            cbbHTownGrp.addItem("--Select group--");
            cbbHTownGrp.addActionListener(ee4);
        }
    }

    public int removeE6() {
        int i = 0;
        if (cbbResidencePrv.getSelectedIndex() == 0) {
            cbbResidenceDis.removeActionListener(ee6);
            cbbResidenceDis.removeAllItems();
            cbbResidenceDis.addItem("--Select District--");
            cbbResidenceDis.addActionListener(ee6);
        } else {
            if (cbbResidenceDis.getItemAt(1) != null) {
                cbbResidenceDis.removeActionListener(ee6);
                cbbResidenceDis.removeAllItems();
                cbbResidenceDis.addItem("--Select District--");
                cbbResidenceDis.addActionListener(ee6);
                //i =1;
            }
            i = 1;
        }

        return i;
    }

    public void removeE7() {
        if (cbbResidenceWrd.getItemAt(1) != null) {
            cbbResidenceWrd.removeActionListener(ee7);
            cbbResidenceWrd.removeAllItems();
            cbbResidenceWrd.addItem("--Select Ward--");
            cbbResidenceWrd.addActionListener(ee7);
        }
    }

    public void removeE8() {
        if (cbbResidenceGrp.getItemAt(1) != null) {
            cbbResidenceGrp.removeActionListener(ee8);
            cbbResidenceGrp.removeAllItems();
            cbbResidenceGrp.addItem("--Select group--");
            cbbResidenceGrp.addActionListener(ee8);
        }
    }

    public class FilterProvinceHTown implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e1) {
            System.out.println("Clicking Province Home Town");
            PersonDAL prsDal = new PersonDAL();
//                if (cbbHTownDis.getSelectedIndex() == 0) {
//                    cbbHTownDis.removeAllItems();
//                    cbbHTownDis.addItem("--Select District--");
            ProvinceEntity Province = (ProvinceEntity) cbbHTownPrv.getSelectedItem();
            System.out.println("ID of province: " + Province.getProvinceID().toString());
            HashSet<DistrictEntity> listDistrict = prsDal.getListDistrict(Province.getProvinceID());
            int i = removeE2();
            if (i == 1) {
                for (DistrictEntity district : listDistrict) {
                    cbbHTownDis.addItem(district);
                }
            }

        }
    }

    public class E2 implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e2) {
            System.out.println("Clicking District");
            //cbbHTownWrd.removeAllItems();
            // cbbHTownWrd.addItem("--Choose Ward--");
            PersonDAL prsDal = new PersonDAL();
            // if (e2.getSource() == cbbHTownDis.getSelectedItem()) {
            DistrictEntity District = (DistrictEntity) cbbHTownDis.getSelectedItem();
            System.out.println("ID of district: " + District.getDistricID().toString());
            HashSet<WardEntity> listWard = prsDal.getListWard(District.getDistricID());
            //
            removeE3();
            for (WardEntity ward : listWard) {
//                    if(cbbHTownWrd!=null & i ==0){
//                            cbbHTownWrd.removeAllItems();
//                            i=1;
//                        }
                cbbHTownWrd.addItem(ward);
            }
            // }
        }
    }

    public class E3 implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e3) {

            System.out.println("Clicking Ward");
            PersonDAL prsDal = new PersonDAL();
            // if (e3.getSource() == cbbHTownWrd.getSelectedItem()) {/
            WardEntity Ward = (WardEntity) cbbHTownWrd.getSelectedItem();
            System.out.println("Id of Ward: " + Ward.getWardID().toString());
            //JOptionPane.showMessageDialog(null,Ward.getWardID());
            HashSet<PopulationGroupEntity> listGroup = prsDal.getListGroup(Ward.getWardID());
            removeE4();
            for (PopulationGroupEntity group : listGroup) {
                cbbHTownGrp.addItem(group);
            }
            //  }
        }
    }

    public class E4 implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("Click Group");
            PopulationGroupEntity group = (PopulationGroupEntity) cbbHTownGrp.getSelectedItem();
            System.out.println("ID group se duoc luu vao csdl....hehehe:  " + group.getPgroupID().toString());
            groupHTown = group.getPgroupID().toString();
        }
    }

    public class FilterProvinceRes implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e1) {
            System.out.println("Clicking Province District");
            PersonDAL prsDal = new PersonDAL();
//                if (cbbHTownDis.getSelectedIndex() == 0) {
//                    cbbHTownDis.removeAllItems();
//                    cbbHTownDis.addItem("--Select District--");
            ProvinceEntity Province = (ProvinceEntity) cbbResidencePrv.getSelectedItem();
            System.out.println("ID of province: " + Province.getProvinceID().toString());
            HashSet<DistrictEntity> listDistrict = prsDal.getListDistrict(Province.getProvinceID());
            int i = removeE6();
            if (i == 1) {
                for (DistrictEntity district : listDistrict) {
                    cbbResidenceDis.addItem(district);
                }
            }

        }
    }

    public class E6 implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("Clicking District Residence");
            PersonDAL prsDal = new PersonDAL();
            DistrictEntity District = (DistrictEntity) cbbResidenceDis.getSelectedItem();
            System.out.println("ID of district: " + District.getDistricID().toString());
            HashSet<WardEntity> listWard = prsDal.getListWard(District.getDistricID());
            removeE7();
            for (WardEntity ward : listWard) {
                cbbResidenceWrd.addItem(ward);
            }
        }
    }

    public class E7 implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("Clicking Ward Residence");
            PersonDAL prsDal = new PersonDAL();
            WardEntity Ward = (WardEntity) cbbResidenceWrd.getSelectedItem();
            System.out.println("Id of Ward: " + Ward.getWardID());
            HashSet<PopulationGroupEntity> listGroup = prsDal.getListGroup(Ward.getWardID());
            removeE8();
            for (PopulationGroupEntity group : listGroup) {
                cbbResidenceGrp.addItem(group);
            }
        }
    }

    public class E8 implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("Click Group Residence");
            PopulationGroupEntity group = (PopulationGroupEntity) cbbResidenceGrp.getSelectedItem();
            System.out.println("ID group of Residence se duoc luu vao csdl....hehehe:  " + group.getPgroupID());
            groupRes = group.getPgroupID().toString();
        }
    }
    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
        // TODO add your handling code here:
        //save to dat file
        int NUM_ARRAY = 10;
        System.out.println("click save");
        BufferedImage[] imagesArr = new BufferedImage[NUM_ARRAY];
        if (iconArr.size() == 10) {
            ProcessFile proFile = new ProcessFile();
            //IconList[] iconArr = lstTrainFace.getCellRenderer();

            for (int i = 0; i < NUM_ARRAY; i++) {
                IconList icon = iconArr.get(i);
                imagesArr[i] = icon.getImage();
            }
            //change to gray image
            ProcessImage proImage = new ProcessImage();
            //create folder tmp
            proFile.createFolder("../CIS_SProjectR2/data/tmp/");
            for (int i = 0; i < NUM_ARRAY; i++) {
                IplImage cvImage = new IplImage();
                cvImage = IplImage.createFrom(imagesArr[i]);
                cvImage = proImage.setGray(cvImage);
                cvSaveImage("../CIS_SProjectR2/data/tmp/" + (i + 1) + ".pgm", cvImage);
            }
            //for(int i = 0; i <)
            proFile.createDatFile(txfIdentifyNumber.getText());
            proFile.deleteFolder("../CIS_SProjectR2/data/tmp/");

            //proFile.saveBuffImageToDAT(imagesArr, txtID.getText(),"../CIS_SProjectR2/data/facedat/");
            System.out.println("Save array of images to dat successfully");
        } else {
            JOptionPane.showConfirmDialog(null, "The number of face is wrong",
                    "Warning",
                    JOptionPane.CLOSED_OPTION);
        }


        //please implement save to database in here
        DateFormat formatter;
        formatter = new SimpleDateFormat("yyyy-mm-dd");
        Calendar currentDate = Calendar.getInstance();
        PersonEntity prsEnt = new PersonEntity();
        PersonDAL prsDal = new PersonDAL();
//        //prsEnt.setPid(txfIdNumber.getText());auto
        prsEnt.setIdentity_number(txfIdentifyNumber.getText());
        prsEnt.setFullname(txfFullname.getText());
        try {
            prsEnt.setDob(formatter.parse(txfDob.getText().toString()));
//            JOptionPane.showMessageDialog(this, txfDOB.getText().toString());
        } catch (ParseException ex) {
            Logger.getLogger(PFaceDetection.class.getName()).log(Level.SEVERE, null, ex);
        }
        if(groupHTown.equals("")==false){
        prsEnt.setHometown(groupHTown);
        }
        //JOptionPane.showMessageDialog(this, groupRes);
        if(groupRes.equals("")==false){
        prsEnt.setPermanent_residence(groupRes);
        }
        prsEnt.setEthnic(txfEthnic.getText());
        prsEnt.setReligion(txfReligion.getText());
        prsEnt.setCharacteristic(txaCharac.getText());
        prsEnt.setSaveImg(tempPath);
        prsEnt.setPid(getIdentityNumber());
        prsDal.insertnewPerson(prsEnt);

        //
    }//GEN-LAST:event_btnSaveActionPerformed

    private void jLabel12MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel12MouseClicked
        // TODO add your handling code here:
         lblLoadImg.setIcon(null);
        jFileChooser1.setDialogTitle("Choose a file");
        //this.getContentPane().add(jFileChooser1);
        jFileChooser1.setVisible(true);
        int returnVal = jFileChooser1.showOpenDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File selectedFile = jFileChooser1.getSelectedFile();
            tempPath = selectedFile.getAbsolutePath();
            int ind = tempPath.lastIndexOf(".");
            if (ind == -1) { // no file type
                JOptionPane.showConfirmDialog(this,
                        "Please choose .jpg or .jpeg type!", "Warning",
                        JOptionPane.CLOSED_OPTION,
                        JOptionPane.WARNING_MESSAGE);
                return;
            }
            String tmp = tempPath.substring(ind);
            if (tmp.equalsIgnoreCase(".jpg") == false // .jpg or jpeg
                    || tmp.equalsIgnoreCase(".jpeg")) {
                JOptionPane.showConfirmDialog(this,
                        "Please choose .jpg or .jpeg type!", "Warning",
                        JOptionPane.CLOSED_OPTION,
                        JOptionPane.WARNING_MESSAGE);
                return;
            }
            //App.objImage.setPath(tempPath);
            BufferedImage image = null;
            File file = new File(tempPath);
            System.out.println(file.getAbsolutePath());
            try {
                image = javax.imageio.ImageIO.read(file);
            } catch (IOException ex) {
                System.out.println("Can't load image " + tempPath);
                JOptionPane.showConfirmDialog(this, "Please reload image!",
                        "Warning", JOptionPane.CLOSED_OPTION,
                        JOptionPane.WARNING_MESSAGE);
            }
            BufferedImage resizeImage = null;
            ProcessImage objImage = new ProcessImage();
            resizeImage = objImage.resize(image, 300, 200);
            lblLoadImg.setIcon(new ImageIcon(resizeImage));
        }
    }//GEN-LAST:event_jLabel12MouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnEdit;
    private javax.swing.JButton btnRestore;
    private javax.swing.JButton btnSave;
    private javax.swing.JButton btnsearch;
    private javax.swing.JComboBox cbbHTownDis;
    private javax.swing.JComboBox cbbHTownGrp;
    private javax.swing.JComboBox cbbHTownPrv;
    private javax.swing.JComboBox cbbHTownWrd;
    private javax.swing.JComboBox cbbResidenceDis;
    private javax.swing.JComboBox cbbResidenceGrp;
    private javax.swing.JComboBox cbbResidencePrv;
    private javax.swing.JComboBox cbbResidenceWrd;
    private javax.swing.JComboBox cbxsearch;
    private javax.swing.Box.Filler filler1;
    public javax.swing.JFileChooser jFileChooser1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane8;
    private javax.swing.JScrollPane jScrollPane9;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JSplitPane jSplitPane2;
    private javax.swing.JSplitPane jSplitPane3;
    private javax.swing.JLabel lblLoadImg;
    public javax.swing.JList lstFace;
    private javax.swing.JPanel pnlEdit;
    private javax.swing.JPanel pnlResult;
    private javax.swing.JScrollPane scrollListFace;
    public javax.swing.JTable tblPersonInfor;
    private javax.swing.JTextArea txaCharac;
    private javax.swing.JTextField txfDob;
    private javax.swing.JTextField txfEthnic;
    private javax.swing.JTextField txfFullname;
    private javax.swing.JTextArea txfHometown;
    public javax.swing.JTextField txfIdentifyNumber;
    private javax.swing.JTextField txfReligion;
    private javax.swing.JTextArea txfResidence;
    private javax.swing.JTextField txfitem;
    // End of variables declaration//GEN-END:variables
}

class ListFaceDataListener extends MouseAdapter {

    JPopupMenu rightMenu = new JPopupMenu("Right menu");
    PSearch searchForm;
    String tempPath = null;

    public ListFaceDataListener(final PSearch searchForm) {
        this.searchForm = searchForm;
        JMenuItem deleteItem = new JMenuItem("Delete this face");
        //addMenu.setIcon(getIconResize("../CIS_SProjectR2/src/icon/Delete_16x16.png"));
        deleteItem.addActionListener(
                new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        System.out.println("right me1");
                        int a = searchForm.lstFace.getSelectedIndex();
                        if (a >= 0) {
                            searchForm.iconArr.remove(a);
                            searchForm.updateFaceCellRender();
                        }
                    }
                });


        rightMenu.add(deleteItem);

        JMenuItem AddItem = new JMenuItem("Add face");
        AddItem.addActionListener(
                new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        searchForm.jFileChooser1.setDialogTitle("Choose a file");
                        searchForm.add(searchForm.jFileChooser1);
                        searchForm.jFileChooser1.setVisible(true);
                        int returnVal = searchForm.jFileChooser1.showOpenDialog(null);

                        if (returnVal == JFileChooser.APPROVE_OPTION) {
                            File selectedFile = searchForm.jFileChooser1.getSelectedFile();
                            tempPath = selectedFile.getAbsolutePath();
                            int ind = tempPath.lastIndexOf(".");
                            if (ind == -1) { // no file type
                                JOptionPane.showConfirmDialog(searchForm,
                                        "Please choose .pgm type!", "Warning",
                                        JOptionPane.CLOSED_OPTION,
                                        JOptionPane.WARNING_MESSAGE);
                                return;
                            }
                            String tmp = tempPath.substring(ind);
                            if (tmp.equalsIgnoreCase(".pgm") == false) {
                                JOptionPane.showConfirmDialog(searchForm,
                                        "Please choose pgm type!", "Warning",
                                        JOptionPane.CLOSED_OPTION,
                                        JOptionPane.WARNING_MESSAGE);
                                return;
                            }
                        }
                        if (tempPath != null) {
                            IplImage ipltemp = cvLoadImage(tempPath);
                            BufferedImage bf = ipltemp.getBufferedImage();
                            IconList ic = new IconList(searchForm.txfIdentifyNumber.getText(), bf, 92, 112);
                            searchForm.iconArr.add(ic);
                            searchForm.updateFaceCellRender();
                        }
                    }
                });
        rightMenu.add(AddItem);
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        switch (e.getModifiers()) {
            case InputEvent.BUTTON1_MASK: {
                if (e.getClickCount() == 2) {
                    System.out.println("double me");
                }
                break;
            }
            case InputEvent.BUTTON2_MASK: {

                System.out.println("That's the MIDDLE button");

                break;
            }
            case InputEvent.BUTTON3_MASK: {
                System.out.println("That's the RIGHT button");
                //rightMenu.pack();
                rightMenu.show(e.getComponent(), e.getX(), e.getY());
                break;
            }
        }
    }
}